import { Scene_Base } from "@/scenes/Scene_Base.js";
import { Window_BattleStatus } from "@/windows/Window_BattleStatus.js";
import { Window_PartyCommand } from "@/windows/Window_PartyCommand.js";
import { Window_ActorCommand } from "@/windows/Window_ActorCommand.js";
import { Window_Help } from "@/windows/Window_Help.js";
import { Window_SkillList } from "@/windows/Window_SkillList.js";
import { Window_BattleItem } from "@/windows/Window_BattleItem.js";
import { Window_BattleTargets } from "@/windows/Window_BattleTargets.js";
import { Window_Message } from "@/windows/Window_Message.js";
import { ScrollText } from "@/containers/ScrollText.js";
import { config } from "@/managers/ConfigManager";

import { Spriteset_Battle } from "@/containers/Spriteset_Battle.js";

import { BattleManager } from "@/managers/BattleManager.js";
import CombatEventBus from "@/Battle/CombatEventBus.js";
import { BattleEventType } from "@/Battle/Event/BattleEvent.js";
//import { AudioManager } from "@/managers/AudioManager.js";

export class Scene_Battle extends Scene_Base{
    constructor(){
        super();
        this._undecideAction=null;
    }

    create(){
        super.create();
        this.createDisplayObjects();
    }

    createDisplayObjects() {
        this.createSpriteset();
        this.createPartyCommandWindow();
        this.createActorCommandWindow();
        this.createStatusWindow();
        this.createHelpWindow();
        this.createSkillWindow();
        this.createItemWindow();
        this.createTargetsWindow();
        this.createMessageWindow();
        this.createScrollTextWindow();
        this.attachHandler();
    }

    createSpriteset(){
        this.spriteset=new Spriteset_Battle();
        this.addChild(this.spriteset);
    }

    createPartyCommandWindow() {
        this._partyCommandWindow = new Window_PartyCommand(192,180);
        this._partyCommandWindow.x=0;
        this._partyCommandWindow.y=config.height-180;
        this._partyCommandWindow.hide();
        this._partyCommandWindow.deactivate();
        this.addWindow(this._partyCommandWindow);
    }

    createActorCommandWindow() {
        this._actorCommandWindow = new Window_ActorCommand(192,180);
        this._actorCommandWindow.x=0;
        this._actorCommandWindow.y=config.height-180;
        this._actorCommandWindow.hide();
        this._actorCommandWindow.deactivate();
        this.addWindow(this._actorCommandWindow);
    }

    createStatusWindow() {
        this._statusWindow = new Window_BattleStatus(config.width-192,180);
        this._statusWindow.x=192;
        this._statusWindow.y=config.height-180;
        this._statusWindow.hide();
        this._statusWindow.deactivate();
        this.addWindow(this._statusWindow);
    }

    createHelpWindow() {
        this._helpWindow = new Window_Help(config.width,96);
        this._helpWindow.hide();
        this.addWindow(this._helpWindow);
    }

    createSkillWindow() {
        let wh = config.height-276;//windowHeight-status and help
        this._skillWindow = new Window_SkillList(config.width, wh);
        this._skillWindow.y=96;
        this._skillWindow.hide();
        this._skillWindow.leave();
        this.addWindow(this._skillWindow);
    }

    createItemWindow() {
        let wh = config.height-276;
        this._itemWindow = new Window_BattleItem(config.width, wh);
        this._itemWindow.y=96;
        this._itemWindow.hide();
        this._itemWindow.leave();
        this.addWindow(this._itemWindow);
    }

    createTargetsWindow(){
        this._targetsWindows = new Window_BattleTargets(config.width-192,180);
        this._targetsWindows.x = 192;
        this._targetsWindows.y=config.height-180;
        this._targetsWindows.hide();
        this._targetsWindows.leave();
        this.addWindow(this._targetsWindows);
    }

    createMessageWindow() {
        this._messageWindow = new Window_Message(config.width,144);
        this._messageWindow.x=0;
        this._messageWindow.y=config.height-144;
        this._messageWindow.hide();
        this.addWindow(this._messageWindow);
    }

    createScrollTextWindow() {
        this._scrollTextWindow = new ScrollText(config.width,config.height);
        this._scrollTextWindow.hide();
        this.addWindow(this._scrollTextWindow);
    }

    attachHandler(){
        this._partyCommandWindow.setHandler('fight',()=>{
            this._partyCommandWindow.close();
            this._actorCommandWindow.open();
            this._actorCommandWindow.activate();
            this._actorCommandWindow.select(-1);
        });
        this._partyCommandWindow.setHandler('escape',()=>{
            //BattleManager.processEscape();
        });

        this._actorCommandWindow.setHandler('attack',()=>{
            this._undecideAction.setSkill(this._battler.attackSkillId());

            this._actorCommandWindow.deactivate();

            this.openTargetsWindow();
        });
        this._actorCommandWindow.setHandler('skill',(item)=>{
            this._actorCommandWindow.deactivate();

            this._skillWindow.setStypeId(item.ext);
            this._skillWindow.open();
            this._skillWindow.activate();
            this._skillWindow.select(-1);
        });
        this._actorCommandWindow.setHandler('guard',()=>{
            this._undecideAction.setSkill(this._battler.guardSkillId());
            this._undecideAction.makeTargets();
            this._actorCommandWindow.close();
        });
        this._actorCommandWindow.setHandler('item',()=>{
            this._actorCommandWindow.deactivate();

            this._itemWindow.open();
            this._itemWindow.activate();
            this._itemWindow.select(-1);
        });
        this._actorCommandWindow.setHandler('cancel',()=>{
            this._actorCommandWindow.close();

            this._partyCommandWindow.open();
            this._partyCommandWindow.activate();
            this._partyCommandWindow.select(-1);
        });

        this._targetsWindows.setHandler('ok',(battler)=>{
            this._targetsWindows.close();
            this._actorCommandWindow.close();

            this._statusWindow.open();
            this._statusWindow.deactivate();
        });
        this._targetsWindows.setHandler('cancel',()=>{
            this._targetsWindows.close();

            this._statusWindow.open();
            this._statusWindow.deactivate();

            this._actorCommandWindow.activate();
        });

        this._itemWindow.setHandler('ok',(item)=>{
            //设置action
            this._undecideAction.setItem(item.id);
            this._itemWindow.close();
            
            if(this._undecideAction.isForAll()){
                this._undecideAction.makeTargets();
                this._actorCommandWindow.close();
                return;
            }

            this.openTargetsWindow();
        });
        this._itemWindow.setHandler('cancel',()=>{
            this._itemWindow.close();
            this._actorCommandWindow.activate();
        });

        this._skillWindow.setHandler('ok',(skill)=>{
            //设置action
            this._undecideAction.setSkill(skill.id);
            this._itemWindow.close();
            
            if(this._undecideAction.isForAll()){
                this._undecideAction.makeTargets();
                this._actorCommandWindow.close();
                return;
            }

            this.openTargetsWindow();
        });
        this._skillWindow.setHandler('cancel',()=>{
            this._skillWindow.close();
            this._actorCommandWindow.activate();
        });
    }

    openTargetsWindow(){
        this._statusWindow.close();
        this._targetsWindows.setAction(this._undecideAction);
        this._targetsWindows.open();
        this._targetsWindows.activate();
        this._targetsWindows.select(-1);
    }

    start(){
        super.start();
        BattleManager.startBattle();
        BattleManager.onInstrusion('input','action-prepare',action=>{
            if(!action)return;
            this._undecideAction=action;
            this._battler=this._undecideAction.subject();
            this._battler.select();
            
            this._actorCommandWindow.setActor(this._battler);
            this._partyCommandWindow.close();
            this._actorCommandWindow.show();
            this._actorCommandWindow.activate();

            this._statusWindow.open();
            this._statusWindow.select(this._battler.index())
        });
        
        // BattleManager.playBattleBgm();
        
    }

    update(){
        //角色是否还在移动，动画是否还在播放
        if(!this.isBusy()){
            BattleManager.update();
        }

        super.update();
    }

    isAnyInputWindowActive(){
        return this._partyCommandWindow.active||
        this._actorCommandWindow.active||
        this._itemWindow.active||
        this._skillWindow.active||
        this._targetsWindows.active;
    }

    isBusy(){
        return super.isBusy()||
        this.isAnyInputWindowActive()||
        this.spriteset.isBusy();
    }

    stop() {
        super.stop();
        this._statusWindow.close();
        this._partyCommandWindow.close();
        this._actorCommandWindow.close();
    }

    terminate() {
        super.terminate();
        $gameParty.onBattleEnd();
        $gameTroop.onBattleEnd();
        BattleManager.destroy();
        //AudioManager.stopMe();
    }
}